home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 401-425 / disk_415 / cbbs / cbbs.lzh / mem / loadram.c < prev    next >
C/C++ Source or Header  |  1990-03-23  |  8KB  |  312 lines

  1. #include <ctype.h>
  2. #include <libraries/dos.h>
  3. #include <exec/memory.h>
  4. #include <time.h>
  5. #include <stdio.h>
  6.  
  7. /* LOADRAM  ramdir diskdir filespec1 [filespec2 ... filespecN]
  8.    where the filespecs can contain a leading or trailing * wildcard
  9.    such as *.c or a* (but not embedded as in a*b)
  10.    All qualified files are copied from the disk directory into the
  11.    ram directory (which is created if it does not exist). Any files
  12.    which match the pattern *.o are copied last so that their file
  13.    date will be later than the corresponding *.c which prevents 'make'
  14.    trying to remake a .o file that is actually up to date.
  15.    This is a fudge, as I am still using 1.2. In 1.3 I think this could
  16.    be fixed up better.
  17.    Pete VE5VA
  18. */
  19.  
  20. /*
  21.  *  FYI ... a directory item looks like this:
  22. struct FileInfoBlock {
  23.    LONG fib_DiskKey;
  24.    LONG fib_DirEntryType;
  25.    char fib_FileName[108];
  26.    LONG fib_Protection;
  27.    LONG fib_EntryType;
  28.    LONG fib_Size;
  29.    LONG fib_NumBlocks;
  30.    struct DateStamp fib_Date;
  31.    char fib_Comment[80];
  32.    char padding[36];
  33. };
  34. */
  35.  
  36. typedef struct FileInfoBlock FCB;
  37. FCB *ifcb = 0,*ofcb = 0;
  38. static int olddta;
  39. long ilock = 0L,olock = 0L;      /* input and output directory locks */
  40. long date[3];
  41. char dirname[100];      /* package the disk dir name */
  42. char ramname[100];      /*          and ram dir name */
  43. char sname[4096];
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char *argv[];
  48. {
  49.    FILE *fd;
  50.    register char *fp,*cp;
  51.    char *wco[1];
  52.    extern char *filename();
  53.    extern char *AllocMem();
  54.    extern long CreateDir();
  55.    extern long diropen();
  56.    sname[0] = 0;
  57.    if(argc < 3)usage();
  58.    /* copy the disk directory name string and make sure it's not too long */
  59.    for(fp=argv[2],cp=dirname;*fp && (cp < &dirname[99]);)*cp++ = *fp++;
  60.    *cp++ = 0;
  61.    if(cp > &dirname[99]) {
  62.       printf("disk directory name too long\n");
  63.       exit(20);
  64.    }
  65.    /* copy the ram directory name string and make sure it's not too long */
  66.    for(fp=argv[1],cp=ramname;*fp && (cp < &ramname[99]);)*cp++ = *fp++;
  67.    *cp++ = 0;
  68.    if(cp > &ramname[99]) {
  69.       printf("ram directory name too long\n");
  70.       exit(20);
  71.    }
  72.    /* This allocates the memory on a longword boundary */
  73.    ifcb = (FCB *)AllocMem((long)sizeof(*ifcb),
  74.                                    (long)MEMF_PUBLIC|MEMF_CLEAR);
  75.    ofcb = (FCB *)AllocMem((long)sizeof(*ofcb),
  76.                                    (long)MEMF_PUBLIC|MEMF_CLEAR);
  77. /*
  78.    if(argv[1][0] == 'q') {
  79.       argc--;
  80.       argv++;
  81.       qflag++;
  82.    }
  83. */
  84.    if((ilock = diropen(argv[1],ifcb)) == 0L) {
  85.       /* Create the ram directory if it isn't already there */
  86.       if((ilock = CreateDir(argv[1])) == 0L) {
  87.          printf("Can't open %s\n",argv[1]);
  88.          done();
  89.       }
  90.    }
  91.    /* Don't need the lock on the ram: directory */
  92.    UnLock(ilock);
  93.    ilock = 0L;
  94.    if((olock = diropen(argv[2],ofcb)) == 0L) {
  95.       printf("Can't open %s\n",argv[2]);
  96.       done();
  97.    }
  98.  
  99.  
  100.    /* Look through the directory and copy the relevant files */
  101.    /* set up the *.o wildcard search */
  102.    wco[0] = "*.o";
  103.    fp = &sname[0];
  104.    while(dirnext(ofcb,olock)) {
  105.       /* Does the next filename match any of the wildcard arguments? */
  106.       if(wcm(argc-3,&ofcb->fib_FileName[0],&argv[3])) {
  107.          /* If this matches "*.o" then save the name and copy it after
  108.             everything else is done so that the *.o files have a later
  109.             created date than the *.c files. This way 'make' won't try
  110.             to remake lots of *.o files because they were copied before
  111.             their corresponding *.c file.
  112.          */
  113.          if(wcm(1,&ofcb->fib_FileName[0],&wco[0])) {
  114.             cp = &ofcb->fib_FileName[0];
  115.             while(*cp)*fp++ = *cp++;
  116.             *fp++ = 0;
  117.          }
  118.          else {
  119.             if(copy(argv[2],argv[1],&ofcb->fib_FileName[0])) {
  120.                printf("I/O error in writing %s\n",ofcb->fib_FileName);
  121.                done();
  122.             }
  123.             else
  124.                printf("%s\n",ofcb->fib_FileName);
  125.          }
  126.       }
  127.    }
  128.    olock = 0L;
  129.    /* Now check the sname list for any *.o files and copy them
  130.    */
  131.    *fp++ = 0;
  132.    for(fp=&sname[0];*fp;) {
  133.       if(copy(argv[2],argv[1],fp)) {
  134.          printf("I/O error in writing %s\n",ofcb->fib_FileName);
  135.          done();
  136.       }
  137.       printf("%s\n",fp);
  138.       while(*fp)fp++;
  139.       fp++;
  140.    }
  141.  
  142.    fp = filename(argv[1],".loadram");
  143.    if((fd = fopen(fp,"w")) == 0) {
  144.       printf("Can't create %s\n",fp);
  145.       done();
  146.    }
  147.    /* write the current date and directory name out and we're done */
  148.    DateStamp(&date[0]);
  149.    fwrite(&date[0],12,1,fd);
  150.    fwrite(dirname,100,1,fd);
  151.    fwrite(ramname,100,1,fd);
  152.    fclose(fd);
  153.    done();
  154. }
  155. done()
  156. {
  157.    freefcb();
  158.    if(ilock)UnLock(ilock);
  159.    if(olock)UnLock(olock);
  160.    exit(0);
  161. }
  162. static char filestr[200];
  163. /* append string b to a to create a filename */
  164. char *filename(a,b)
  165. char *a,*b;
  166. {
  167.    register char *p,*q;
  168.    p = filestr;
  169.    q = a;
  170.    while(*q)*p++ = *q++;
  171.    q--;
  172.    if(*q != ':')*p++ = '/';
  173.    q = b;
  174.    while(*q)*p++ = *q++;
  175.    *p = 0;
  176.    return(filestr);
  177. }
  178. usage()
  179. {
  180.    printf("usage: loadram ramdir diskdir [filespec1, filespec2 ...]\n");
  181.    freefcb();
  182. }
  183.  
  184. /*
  185.  *  Open directory and return a Lock or zero, if it fails the lock is
  186.  *  unlocked
  187.  */
  188. extern long Lock(),Examine(),ExNext();
  189. long diropen(cp,fcb)
  190. char   *cp;
  191. FCB *fcb;
  192. {
  193.    long  result,lock;
  194.    /* Get the lock on the directory */
  195.    lock = Lock(cp,ACCESS_READ);
  196.    if(lock == 0L) {
  197.       return (0L);
  198.    }
  199.    /* Examine the lock and if that fails or if this is not a directory
  200.       then give up
  201.    */
  202.    result = Examine(lock,fcb);
  203.    if((result == 0L) || (fcb->fib_DirEntryType < 0)) {
  204.       UnLock(lock);
  205.       return(0L);
  206.    }
  207.    /* Looks good */
  208.    return(lock);
  209. }
  210. freefcb()
  211. {
  212.    if(ifcb)FreeMem(ifcb,(long)sizeof(*ifcb));
  213.    if(ofcb)FreeMem(ofcb,(long)sizeof(*ofcb));
  214. }
  215. /*
  216.  *  Return next file entry, ignore directories and when the lock
  217.  *  is exhausted Unlock it.
  218.  */
  219.  
  220. dirnext(fcb,lock)
  221. FCB *fcb;
  222. long lock;
  223. {
  224.    /* search for next file */
  225.    while(1) {
  226.  
  227.       if(ExNext(lock,fcb) == 0) {
  228.          UnLock(lock);
  229.          return(0);
  230.       }
  231.       /* If it is a directory ignore it */
  232.       if(fcb->fib_DirEntryType >= 0) continue;
  233.       return(1);
  234.    }
  235. }
  236. wcm(n,p,argv)
  237. int n;
  238. char *p;
  239. char *argv[];
  240. {
  241.  
  242.    if(n <= 0)return(1);
  243.    n--;
  244.    do {
  245.       if(match(p,argv[n]))return(1);
  246.    }while(n--);
  247.    return(0);
  248. }
  249. match(a,b)
  250. char *a,*b;
  251. {
  252.    register char *p,*q;
  253.  
  254.    p = a;
  255.    q = b;
  256.    if(*q == '*') {
  257.       /* Leading wildcard * in the pattern */
  258.       while(*p)p++;
  259.       while(*q)q++;
  260.       p--;
  261.       q--;
  262.       while(*q != '*') {
  263.          if(*p == *q) {
  264.             p--;
  265.             q--;
  266.             continue;
  267.          }
  268.          break;
  269.       }
  270.       if(*q == '*')return(1);
  271.       return(0);
  272.    }
  273.    while(*p && *q && (*q != '*')) {
  274.       if(*p == *q) {
  275.          p++;
  276.          q++;
  277.          continue;
  278.       }
  279.       break;
  280.    }
  281.    if((*p == 0) && (*q == 0))return(1);
  282.    if(*q == '*')return(1);
  283.    return(0);
  284. }
  285. char iobuf[512];
  286.  
  287. /* copy file from disk to ram: */
  288. copy(inf,outf,name)
  289. char *inf,*outf,*name;
  290. {
  291.    FILE *fdi,*fdo;
  292.    int n;
  293.    if((fdi = fopen(filename(inf,name),"r")) == NULL) {
  294.       printf("Can't open %s\n",filestr);
  295.       return;
  296.    }
  297.    if((fdo = fopen(filename(outf,name),"w")) == NULL) {
  298.       printf("Can't open %s\n",filestr);
  299.       return;
  300.    }
  301.    while(n = fread(iobuf,1,512,fdi)) {
  302.       if(fwrite(iobuf,1,n,fdo) != n) {
  303.          fclose(fdi);
  304.          fclose(fdo);
  305.          return(1);
  306.       }
  307.    }
  308.    fclose(fdi);
  309.    fclose(fdo);
  310.    return(0);
  311. }
  312.